home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / primer.doc < prev    next >
Text File  |  1987-03-22  |  49KB  |  1,783 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.                       An Assembly Language Primer
  14.  
  15.                        (C) 1983 by David Whitman
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                     TABLE OF CONTENTS
  71.  
  72.  
  73.       Introduction.......................................2
  74.  
  75.       The Computer As A Bit Pattern Manipulator..........3
  76.  
  77.       Digression: A Notation System for Bit Patterns.....5
  78.  
  79.       Addressing Memory..................................7
  80.  
  81.       The Contents of Memory: Data and Programs..........8
  82.  
  83.       The Dawn of Assembly Language......................9
  84.  
  85.       The 8088..........................................11
  86.  
  87.       Assembly Language Syntax..........................14
  88.  
  89.       The Stack.........................................17
  90.  
  91.       Software Interrupts...............................19
  92.  
  93.       Pseudo-Operations.................................21
  94.  
  95.       Tutorial..........................................23
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                                                                       2
  135.  
  136.       >>INTRODUCTION<<
  137.  
  138.       Many people requesting CHASM have indicated that they are
  139.       interested in *learning* assembly language.  They are beginners,
  140.       and have little idea just where to start.  This primer is
  141.       directed to those users.  Experienced users will probably find
  142.       little here that they do not already know.
  143.  
  144.       Being a primer, this text will not teach you everything there is
  145.       to know about assembly language programming.  It's purpose is to
  146.       give you some of the vocabulary and general ideas which will help
  147.       you on your way.
  148.  
  149.       I must make a small caveat: I consider myself a relative beginner
  150.       in assembly language programming.  A big part of the reason for
  151.       writing CHASM was to try and learn this branch of programming
  152.       from the inside out.  I think I've learned quite a bit, but it's
  153.       quite possible that some of the ideas I relate here may have some
  154.       small, or even large, flaws in them.  Nonetheless, I have
  155.       produced a number of working assembly language programs by
  156.       following the ideas presented here.
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                                                                       3
  201.  
  202.       >>THE COMPUTER AS A BIT PATTERN MANIPULATOR.<<
  203.  
  204.       We all have some conception about what a computer does.  On one
  205.       level, it may be thought of as a machine which can execute BASIC
  206.       programs.  Another idea is that the computer is a number
  207.       crunching device.  As I write this primer, I'm using my computer
  208.       as a word processor.
  209.  
  210.       I'd like to introduce a more general concept of just what sort of
  211.       machine a computer is: a bit pattern manipulator.
  212.  
  213.       I'm certain that everyone has been introduced to the idea of a
  214.       *bit*.  (Note: Throughout this primer, a word enclosed in
  215.       *asterisks* is to be read as if it were in italics.)  A bit has
  216.       two states: on and off, typically represented with the symbols
  217.       "1"  and "0".  In this context, DON'T think of 1 and 0 as
  218.       numbers.  They are merely convenient shorthand labels for the
  219.       state of a bit.
  220.  
  221.       The memory of your computer consists of a huge collection of
  222.       bits, each of which could be in either the 1 or 0 (on or off)
  223.       state.
  224.  
  225.       At the heart of your computer is an 8088 microprocessor chip,
  226.       made by Intel.  What this chip can do is manipulate the bits
  227.       which make up the memory of the computer.
  228.  
  229.       The 8088 likes to handle bits in chunks, and so we'll introduce
  230.       special names for the two sizes of bit chunks the 8088 is most
  231.       happy with.  A *byte* will refer to a collection of eight bits.
  232.       A *word* consists of two bytes, or equivalently, sixteen bits.
  233.  
  234.       A collection of bits holds a pattern, determined by the state of
  235.       its individual bits.  Here are some typical byte long patterns:
  236.  
  237.       10101010         11111111         00001111
  238.  
  239.       If you've had a course in probability, it's quite easy to work
  240.       out that there are 256 possible patterns that a byte could hold.
  241.       similarly, a word can hold 65,536 different patterns.
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                                                                       4
  267.  
  268.       All right, now for the single most important idea in assembly
  269.       language programming.  Are you sitting down?  These bit patterns
  270.       can be used to represent other sets of things, by mapping each
  271.       pattern onto a member of the other set.  Doesn't sound like much,
  272.       but IBM has made *BILLIONS* off this idea.
  273.  
  274.       For example, by mapping the patterns a word can hold onto the set
  275.       of integers, you can represent either the numbers from 0 to 65535
  276.       or -32768 to 32767, depending on the exact mapping you use.  You
  277.       might recognize these number ranges as the range of possible line
  278.       numbers, and the possible values of an integer variable, in BASIC
  279.       programs.  This explains these somewhat arbitrary seeming limits:
  280.       BASIC uses words of memory to hold line numbers and integer
  281.       variables.
  282.  
  283.       As another example, you could map the patterns a byte can hold
  284.       onto a series of arbitrarily chosen little pictures which might
  285.       be displayed on a video screen.  If you look in appendix G of
  286.       your BASIC manual, you'll notice that there are *exactly* 256
  287.       different characters that can be displayed on your screen.  Your
  288.       computer uses a byte of memory to tell it what character to
  289.       display at each location of the video screen.
  290.  
  291.       Without getting too far ahead of myself, I'll just casually
  292.       mention that there are about 256 fundamental ways the 8088 can
  293.       manipulate the bit patterns stored in memory.  This suggests
  294.       another mapping which we'll discuss in more detail later.
  295.  
  296.       The point of this discussion is that we can use bit patterns to
  297.       represent anything we want, and by manipulating the patterns in
  298.       different ways, we can produce results which have significance in
  299.       terms of what we're choosing to represent.
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                                                                       5
  333.  
  334.       >>DIGRESSION: A NOTATION SYSTEM FOR BIT PATTERNS<<
  335.  
  336.       Because of their importance, it would be nice to have a
  337.       convenient way to represent the various bit patterns we'll be
  338.       talking about.  We already have one way, by listing the states of
  339.       the individual bits as a series of 1's and 0's.  This system is
  340.       somewhat clumsy, and error prone.  Are the following word
  341.       patterns identical or different?
  342.  
  343.       1111111011111111                         1111111101111111
  344.  
  345.       You probably had trouble telling them apart.  It's easier to tell
  346.       that they're different by breaking them down into more manageable
  347.       pieces, and comparing the pieces.  Here are the same two patterns
  348.       broken down into four bit chunks:
  349.  
  350.       1111 1110 1111 1111                  1111 1111 0111 1111
  351.  
  352.       Some clown has given the name *nybble* to a chunk of 4 bits,
  353.       presumably because 4 bits are half a byte.  A nybble is fairly
  354.       easy to handle.  There are only 16 possible nybble long patterns,
  355.       and most people can distinguish between the patterns quite
  356.       easily.
  357.  
  358.       Each nybble pattern has been given a unique symbol agreed upon by
  359.       computer scientists.  The first 10 patterns were given symbols
  360.       "0" through "9", and when they ran out of digit style symbols,
  361.       they used the letters "A" through "F" for the last six patterns.
  362.       Below is the "nybble pattern code":
  363.  
  364.       0000 = 0    0001 = 1    0010 = 2    0011 = 3
  365.  
  366.       0100 = 4    0101 = 5    0110 = 6    0111 = 7
  367.  
  368.       1000 = 8    1001 = 9    1010 = A    1011 = B
  369.  
  370.       1100 = C    1101 = D    1110 = E    1111 = F
  371.  
  372.       Using the nybble code, we can represent the two similar word
  373.       patterns given above, with the following more manageable
  374.       shorthand versions:
  375.  
  376.                      FEFF       FF7F
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                                                                       6
  399.  
  400.       Of course, the assignment of the symbols for the various nybble
  401.       patterns was not so arbitrary as I've tried to make it appear.  A
  402.       perceptive reader who has been exposed to binary numbers will
  403.       have noticed an underlying system to the assignments.  If the 1's
  404.       and 0's of the patterns are interpreted as actual *numbers*,
  405.       rather than mere symbols for bit states, the first 10 patterns
  406.       correspond to binary numbers whose decimal representation is the
  407.       symbol assigned to the pattern.
  408.  
  409.       The last six patterns receive the symbols "A" through "F", and
  410.       taken together, the symbols 0 through F constitute the digits of
  411.       the *hexadecimal* number system.  Thus, the symbols assigned to
  412.       the different nybble patterns were born out of historical
  413.       prejudice in thinking of the computer as strictly a number
  414.       handling machine.  Although this is an important interpretation
  415.       of these symbols, for the time being it's enough to merely think
  416.       of them as a shorthand way to write down bit patterns.
  417.  
  418.       Because some nybble patterns can look just like a number, it's
  419.       often necessary to somehow indicate that we're talking about a
  420.       pattern.  In BASIC, you do this by adding the characters &H to
  421.       the beginning of the pattern: &H1234.  A more common convention
  422.       is to just add the letter H to the end of the pattern: 1234H.  In
  423.       both conventions, the H is referring to hexadecimal.
  424.  
  425.       Eventually you'll want to learn about using the hexadecimal
  426.       number system, since it is an important way to use bit patterns.
  427.       I'm not going to discuss it in this primer, because a number of
  428.       books have much better treatments of this topic than I could
  429.       produce.  Consider this an advanced topic you'll want to fill in
  430.       later.
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.                                                                       7
  465.  
  466.       >>ADDRESSING MEMORY<<
  467.  
  468.       As stated before, the 8088 chip inside your computer can
  469.       manipulate the bit patterns which make up the computer's memory.
  470.       Some of the possible manipulations are copying patterns from one
  471.       place to another, turning on or turning off certain bits, or
  472.       interpreting the patterns as numbers and performing arithmetic
  473.       operations on them.  To perform any of these actions, the 8088
  474.       has to know what part of memory is to be worked on.  A specific
  475.       location in memory is identified by its *address*.
  476.  
  477.       An address is a pointer into memory.  Each address points to the
  478.       beginning of a byte long chunk of memory.  The 8088 has the
  479.       capability to distinguish 1,048,576 different bytes of memory.
  480.  
  481.       By this point, it probably comes as no surprise to hear that
  482.       addresses are represented as patterns of bits.  It takes 20 bits
  483.       to get a total of 1,048,576 different patterns, and thus an
  484.       address may be written down as a series of 5 nybble codes.  For
  485.       example, DOS stores a pattern which encodes information about
  486.       what equipment is installed on your IBM PC in the word which
  487.       begins at location 00410.  Interpreting the address as a hex
  488.       number, the second byte of this word has an address 1 greater
  489.       than 00410, or 00411.
  490.  
  491.       The 8088 isn't very happy handling 20 bits at a time.  The
  492.       biggest chunk that's convenient for it to use is a 16 bit word.
  493.       The 8088 actually calculates 20 bit addresses as the combination
  494.       of two words, a segment word and an offset word.  The combination
  495.       process involves interpreting the two patterns as hexadecimal
  496.       numbers and adding them.  The way that two 16 bit patterns can be
  497.       combined to give one 20 bit pattern is that the two patterns are
  498.       added out of alignment by one nybble:
  499.  
  500.           0040      4 nybble segment
  501.            0010     4 nybble offset
  502.          --------
  503.           00410     5 nybble address
  504.  
  505.       Because of this mechanism for calculating addresses, they will
  506.       often be written down in what may be called segment:offset form.
  507.       Thus, the address in above calculation could be written:
  508.  
  509.       0040:0010
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.                                                                       8
  531.  
  532.       >>MEMORY CONTENTS: DATA AND PROGRAMS<<
  533.  
  534.       The contents of memory may be broken down into two broad classes.
  535.       The first is *data*, just raw patterns of bits for the 8088 to
  536.       work on.  The significance of the patterns is determined by what
  537.       the computer is being used for at any given time.
  538.  
  539.       The second class of memory contents are *instructions*.  The 8088
  540.       can look at memory and interpret a pattern it sees there as
  541.       specifying one of the 200 some fundamental operations it knows
  542.       how to do.  This mapping of patterns onto operations is called
  543.       the *machine language* of the 8088.  A machine language *program*
  544.       consists of a series of patterns located in consecutive memory
  545.       locations, whose corresponding operations perform some useful
  546.       process.
  547.  
  548.       Note that there is no way for the 8088 to know whether a given
  549.       pattern is meant to be an instruction, or a piece of data to
  550.       operate on.  It is quite possible for the chip to accidentally
  551.       begin reading what was intended to be data, and interpret it as a
  552.       program.  Some pretty bizarre things can occur when this happens.
  553.       In assembly language programming circles, this is known as
  554.       "crashing the system".
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.                                                                       9
  597.  
  598.       >>THE DAWN OF ASSEMBLY LANGUAGE<<
  599.  
  600.       Unless you happen to be an 8088 chip, the patterns which make up
  601.       a machine language program can be pretty incomprehensible.  For
  602.       example, the pattern which tells the 8088 to flip all the bits in
  603.       the byte at address 5555 is:
  604.  
  605.       F6 16 55 55
  606.  
  607.       which is not very informative, although you can see the 5555
  608.       address in there.  In ancient history, the old wood-burning and
  609.       vacuum tube computers were programmed by laboriously figuring out
  610.       bit patterns which represented the series of instructions
  611.       desired.  Needless to say, this technique was incredibly tedious,
  612.       and very prone to making errors.  It finally occurred to these
  613.       ancestral programmers that they could give the task of figuring
  614.       out the proper patterns to the computer itself, and assembly
  615.       language programming was born.
  616.  
  617.       Assembly language represents each of the many operations that the
  618.       computer can do with a *mnemonic*, a short, easy to remember
  619.       series of letters.  For example, in boolean algebra, the logical
  620.       operation which inverts the state of a bit is called "not", and
  621.       hence the assembly language equivalent of the preceding machine
  622.       language pattern is:
  623.  
  624.           NOTB [5555]
  625.  
  626.       The brackets around the 5555 roughly mean "the memory location
  627.       addressed by".  The "B" at the end of "NOTB" indicates that we
  628.       want to operate on a byte of memory, not a word.
  629.  
  630.       Unfortunately, the 8088 can't make head nor tail of the string of
  631.       characters "NOTB".  What's needed is a special program to run on
  632.       the 8088 which converts the string "NOTB" into the pattern F6 16.
  633.       This program is called an assembler.  A good analogy is that an
  634.       assembler program is like a meat grinder which takes in assembly
  635.       language and gives out machine language.
  636.  
  637.       Typically, an assembler reads a file of assembly language and
  638.       translates it one line at a time, outputting a file of machine
  639.       language.  Often times the input file is called the *source file*
  640.       and the output file is called the *object file*.  The machine
  641.       language patterns produced are called the *object code*.
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.                                                                      10
  663.  
  664.       Also produced during the assembly process is a *listing*, which
  665.       summarizes the results of the assembly process.  The listing
  666.       shows each line from the source file, along with the shorthand
  667.       "nybble code" representation of the object code produced.  In the
  668.       event that the assembler was unable to understand any of the
  669.       source lines, it inserts error messages in the listing, pointing
  670.       out the problem.
  671.  
  672.       The primeval assembly language programmers had to write their
  673.       assembler programs in machine language, because they had no other
  674.       choice.  Not being a masochist, I wrote CHASM in BASIC.  When you
  675.       think about it, there's a sort of circular logic in action here.
  676.       Some programmers at Microsoft wrote the BASIC interpreter in
  677.       assembly language, and I used BASIC to write an assembler.
  678.       Someday, I hope to use the present version of CHASM to produce a
  679.       machine language version, which will run about a hundred times
  680.       faster, and at the same time bring this crazy process full
  681.       circle.
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.                                                                      11
  729.  
  730.       >>THE 8088<<
  731.  
  732.       The preceding discussions have (I hope) given you some very
  733.       general background, a world view if you will, about assembly and
  734.       machine language programming.  At this point, I'd like to get
  735.       into a little more detail, beginning by examining the internal
  736.       structure of the 8088 microprocessor, from the programmer's point
  737.       of view.  This discussion is a condensation of information which
  738.       I obtained from "The 8086 Book" which was written by Russell
  739.       Rector and George Alexy, and published by Osborne/McGraw-Hill.
  740.       Once you've digested this, I'd recommend going to The 8086 Book
  741.       for a deeper treatment.  To use the CHASM assembler, you're going
  742.       to need The 8086 Book anyway, to tell you the different 8088
  743.       instructions and their mnemonics.
  744.  
  745.       Inside the 8088 are a number of *registers* each of which can
  746.       hold a 16 bit pattern.  In assembly language, each of the
  747.       registers has a two letter mnemonic name.  There are 14
  748.       registers, and their mnemonics are:
  749.  
  750.       AX BX CX DX     SP BP    SI DI     CS DS SS ES    PC ST
  751.  
  752.       Each of the registers are a little different and have different
  753.       intended uses, but they can be grouped into some broad classes.
  754.  
  755.       The *general purpose* registers (AX BX CX DX) are just that.
  756.       These are registers which hold patterns pulled in from memory
  757.       which are to be worked on within the 8088.  You can use these
  758.       registers for just about anything you want.
  759.  
  760.       Each of the general purpose registers can be broken down into two
  761.       8 bit registers, which have names of their own.  Thus, the CX
  762.       register is broken down into the CH and CL registers.  The "H"
  763.       and "L" stand for high and low respectively.  Each general
  764.       purpose register breaks down into a high/low pair.
  765.  
  766.       The AX register, and its 8 bit low half, the AL register, are
  767.       somewhat special.  Mainly for historical reasons, these registers
  768.       are referred to as the 16 bit and 8 bit *accumulators*.  Some
  769.       operations of the 8088 can only be carried out on the contents of
  770.       the accumulators, and many others are faster when used in
  771.       conjunction with these registers.
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.                                                                      12
  795.  
  796.       Another group of registers are the *segment* registers (CS DS SS
  797.       ES).  These registers hold segment values for use in calculating
  798.       memory addresses.  The CS, or code segment register, is used
  799.       every  time the 8088 accesses memory to read an instruction
  800.       pattern.  The  DS, or data segment register, is used for bringing
  801.       data patterns in.  The SS register is used to access the stack
  802.       (more about the stack later).  The ES is the extra segment
  803.       register.  A very few special instructions use the ES register to
  804.       access memory, plus you can override use of the DS register and
  805.       substitute the ES register, if you need to maintain two separate
  806.       data areas.
  807.  
  808.       The *pointer* (SP BP) and *index* (DI SI) registers are used to
  809.       provide indirect addressing, which is an very powerful technique
  810.       for accessing memory.  Indirect addressing is beyond the scope of
  811.       this little primer, but is discussed in The 8086 Book.  The SP
  812.       register is used to implement a stack in memory. (again, more
  813.       about the stack later)  Besides their special function, the BP,
  814.       DI and SI registers can be used as additional general purpose
  815.       registers.  Although it's physically possible to directly
  816.       manipulate the value in the SP register, it's best to leave it
  817.       alone, since you could wipe out the stack.
  818.  
  819.       Finally, there are two registers which are relatively
  820.       inaccessible to direct manipulation.  The first is the *program
  821.       counter*, PC.  This register always contains the offset part of
  822.       the address of the next instruction to be executed.  Although
  823.       you're not allowed to just move values into this register, you
  824.       *can* indirectly affect its contents, and hence the next
  825.       instruction to be executed, using operations which are equivalent
  826.       to BASIC's GOTO and GOSUB instructions.  Occasionally, you will
  827.       see the PC referred to as the *IP*, which stands for instruction
  828.       pointer.
  829.  
  830.       The last register is also relatively inaccessible.  This is the
  831.       *status* register, ST.  This one has a *two* alternate names, so
  832.       watch for FL (flag register) and PSW (program status word).  The
  833.       latter is somewhat steeped in history, since this was the name
  834.       given to a special location in memory which served a similar
  835.       function on the antique IBM 360 mainframe.
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.                                                                      13
  861.  
  862.       The status register consists of a series of one bit *flags* which
  863.       can affect how the 8088 works.  There are special instructions
  864.       which allow you to set or clear each of these flags.  In
  865.       addition, many instructions affect the state of the flags,
  866.       depending on their outcome.  For example, one of the bits of the
  867.       status register is called the Zero flag.  Any operation which
  868.       ends up generating a bit pattern of all 0's automatically sets
  869.       the Zero flag on.
  870.  
  871.       Setting the flags doesn't seem to do much, until you know that
  872.       there a whole set of conditional branching instructions which
  873.       cause the equivalent to a BASIC GOTO if the particular flag
  874.       pattern they look for is set.  In assembly language, the only way
  875.       to make a decision and branch accordingly is via this flag
  876.       testing mechanism.
  877.  
  878.       Although some instructions implicitly affect the flags, there are
  879.       a series of instructions whose *only* effect is to set the flags,
  880.       based on some test or comparison.  It's very common to see one
  881.       of these comparison operations used to set the flags just before
  882.       a conditional branch.  Taken together, the two instructions are
  883.       exactly equivalent to BASIC's:
  884.  
  885.       IF (comparison) THEN GOTO (linenumber)
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.                                                                      14
  927.  
  928.       >>ASSEMBLY LANGUAGE SYNTAX<<
  929.  
  930.       In general, each line of an assembly language program translates
  931.       to a set of patterns which specify one fundamental operation for
  932.       the 8088 to carry out.
  933.  
  934.       Each line may consist of one or more of the following parts:
  935.  
  936.       First, a label, which is just a marker for the assembler to use.
  937.       If you want to branch to an instruction from some other part of
  938.       the program, you put a label on the instruction.  When you want
  939.       to branch, you refer to the label.  In general, the label can be
  940.       any string of characters you want.  A good practice is to use a
  941.       name which reminds you what that particular part of the program
  942.       does. CHASM will assume that any string of characters which
  943.       starts in the first column of a line is intended to be a label.
  944.  
  945.       After the label, or if the text of the line starts to the right
  946.       of the first column, at the beginning of the text, comes an
  947.       instruction mnemonic.  This specifies the operation that the line
  948.       is asking for.  For a list of the 200-odd mnemonics, along with
  949.       the instructions they stand for, see The 8086 Book.
  950.  
  951.       Most of the 8088 instructions require that you specify one or
  952.       more *operands*.  The operands are what the operation is to work
  953.       on, and are listed after the instruction mnemonic.
  954.  
  955.       There are a number of possible operands.  Probably the most
  956.       common are registers, specified by their two letter mnemonics.
  957.  
  958.       Another operand type is *immediate data*, a pattern of bits to be
  959.       put somewhere or compared or combined with some other pattern.
  960.       Generally immediate data is specified by its nybble code
  961.       representation, marked as such by following it with the letter
  962.       "H".  Some assemblers allow alternate ways to specify immediate
  963.       data which emphasize the pattern's intended use.  CHASM
  964.       recognizes five different ways to represent immediate data.
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.                                                                      15
  993.  
  994.       A memory location can be used as an operand.  We've seen one way
  995.       to do this, by enclosing its address in brackets.  (You can now
  996.       see why the brackets are needed.  Without them, you couldn't
  997.       distinguish between an address and immediate data.)  If you've
  998.       asked the assembler to set aside a section of memory for data
  999.       (more on this latter), and put a label on the request, you can
  1000.       specify that point in memory by using the label.  Finally, there
  1001.       are a number of indirect ways to address memory locations, which
  1002.       you can read about in The 8086 Book.
  1003.  
  1004.       The last major type of operands are labels.  Branching
  1005.       instructions require an operand to tell them where to branch
  1006.       *to*.  In assembly language, you specify locations which may be
  1007.       branched to by putting a label on them.  You can then use the
  1008.       label as an operand on branches.
  1009.  
  1010.       Often times, the order in which the operands are listed can be
  1011.       important.  For example, when moving a pattern from one place to
  1012.       another, you need to specify where the pattern is to come from,
  1013.       and where it's going.  The convention in general use is that the
  1014.       first operand is the *destination* and the second is the
  1015.       *source*.  Thus, to move the pattern in the DX register into the
  1016.       AX register, you would write:
  1017.  
  1018.               MOV AX,DX
  1019.  
  1020.       This may take some getting used to, since when reading from left
  1021.       to right it seems reasonable to assume that the transfer goes in
  1022.       this direction as well.  However, since this convention is pretty
  1023.       well entrenched in the assembly language community, CHASM goes
  1024.       along with it.
  1025.  
  1026.       The last part of an assembly language line is a *comment*.  The
  1027.       comment is totally ignored by the assembler, but is *vital* for
  1028.       humans who are attempting to understand the program.  Assembly
  1029.       language programs tend to be very hard to follow, and so it's
  1030.       particularly important to put in lots of comments so that you'll
  1031.       remember just what it was you were trying to do with a given
  1032.       piece of code.  Professional assembly language programmers put a
  1033.       comment on *every* line of code, explaining what it does, plus
  1034.       devoting many entire lines for additional explanations.  For an
  1035.       example, you should examine the BIOS source listing given in the
  1036.       IBM Technical Reference manual.  Over *half* the text consists of
  1037.       comments!
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.                                                                      16
  1059.  
  1060.       Since the assembler ignores the comments, they cost you nothing
  1061.       in terms of size or speed of execution in the resulting machine
  1062.       language program.  This is in sharp contrast to BASIC, where each
  1063.       remark slows your program down and eats up precious memory.
  1064.  
  1065.       Generally, a character is set aside to indicate to the assembler
  1066.       the beginning of a comment, so that it knows to skip over.  CHASM
  1067.       follows a common convention of reserving the semi-colon (;) for
  1068.       marking comments.
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.                                                                      17
  1125.  
  1126.       >>THE STACK<<
  1127.  
  1128.       I've been dropping the name *stack* from time to time.  The stack
  1129.       is just a portion of memory which has been temporarily set aside
  1130.       to be used in a special way.
  1131.  
  1132.       To get a picture of how the stack works, think of the spring
  1133.       loaded contraptions you sometimes see holding trays in a
  1134.       cafeteria.  As each tray is washed, the busboy puts it on top of
  1135.       the stack in the contraption.  Because the thing is spring
  1136.       loaded, the whole stack sinks down from the weight of the new
  1137.       tray, and the top of the stack ends up always being the same
  1138.       height off the floor.  When a customer takes a tray off the
  1139.       stack, the next one rises up to take its place.
  1140.  
  1141.       In the computer, the stack is used to hold data patterns, which
  1142.       are generally being passed from one program or subroutine to
  1143.       another.  By putting things on the stack, the receiving routine
  1144.       doesn't need to know a particular address to look for the
  1145.       information it needs, it just pulls them off the top of the
  1146.       stack.
  1147.  
  1148.       There is some jargon associated with use of the stack.  Patterns
  1149.       are *pushed* onto the stack, and *popped* off.  Accordingly,
  1150.       there are a set of PUSH and POP instructions in the 8088's
  1151.       repertoire.
  1152.  
  1153.       Because you don't need to keep track of where the patterns are
  1154.       actually being kept, the stack is often used as a scratch pad
  1155.       area, patterns being pushed when the register they're in is
  1156.       needed for some other purpose, then popped out when the register
  1157.       is free.  It's very common for the first few instructions of a
  1158.       subroutine to be a series of pushes to save the patterns which
  1159.       are occupying the registers it's about to use.   This is referred
  1160.       to as *saving the state* of the registers.  The last thing the
  1161.       subroutine will do is pop the patterns back into the registers
  1162.       they came from, thus *restoring the state* of the registers.
  1163.  
  1164.       Following the analogy of the cafeteria contraption, when you pop
  1165.       the stack, the pattern you get is the last one which was pushed.
  1166.       When you pop a pattern off, the next-to-last thing pushed
  1167.       automatically moves to the top, just as the trays rise up when a
  1168.       customer removes one.  Everything comes off the stack in the
  1169.       reverse order of which they went on.  Sometimes you'll see the
  1170.       phrase "last in, first out" or *LIFO stack*.
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.                                                                      18
  1191.  
  1192.  
  1193.       Of course, there are no special spring loaded memory locations
  1194.       inside the computer.  The stack is implemented using a register
  1195.       which keeps track of where the top of the stack is currently
  1196.       located.  When you push something, the pointer is moved to the
  1197.       next available memory location, and the pattern is put in that
  1198.       spot.  When something is popped, it is copied from the location
  1199.       pointed at, then the pointer is moved back.  You don't have to
  1200.       worry about moving the pointer because it's all done
  1201.       automatically with the push and pop instructions.
  1202.  
  1203.       The register set aside to hold the pointer is SP, and that's why
  1204.       you don't want to monkey with SP.  You'll recall that to form an
  1205.       address, two words are needed, an offset and a segment.  The
  1206.       segment word for the stack is kept in the SS register, so you
  1207.       should leave SS alone as well.  When you run the type of machine
  1208.       language program that CHASM produces, DOS will automatically set
  1209.       the SP and SS registers to reserve a stack capable of holding 128
  1210.       words.
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.                                                                      19
  1257.  
  1258.       >>SOFTWARE INTERRUPTS<<
  1259.  
  1260.       I have been religiously avoiding talking about the various
  1261.       individual instructions the 8088 can carry out, because if I
  1262.       didn't, this little primer would soon grow into a rather long
  1263.       book.  However, there's one very important instruction, which
  1264.       when you read about it in The 8088 Book, won't seem particularly
  1265.       useful.  This section will discuss the *software interrupt*
  1266.       instruction, INT, and why it's so important.
  1267.  
  1268.       The 8088 reserves the first 1024 bytes of memory for a series of
  1269.       256 *interrupt vectors*.  Each of these two word long interrupt
  1270.       vectors is used to store the segment:offset address of a location
  1271.       in memory.  When you execute a software interrupt instruction,
  1272.       the the 8088 pushes the location of the next instruction of your
  1273.       program onto the stack, then branches to the memory location
  1274.       pointed at by the vector specified in the interrupt.
  1275.  
  1276.       This probably seems like a rather awkward way to branch around in
  1277.       memory, and chances are you'd never use this method to get from
  1278.       one part of your program to another.  The way these instructions
  1279.       become important is that IBM has pre-loaded a whole series of
  1280.       useful little (and not so little) machine language routines into
  1281.       your computer, and set the interrupt vectors to point to them.
  1282.       All of these routines are set up so that after doing their thing,
  1283.       they use the location pushed on the stack by the interrupt
  1284.       instruction to branch back to your program.
  1285.  
  1286.       Some of these routines are a part of DOS, and documentation for
  1287.       them can be found in Appendix D of the DOS manual.  The rest of
  1288.       them are stored in ROM (read only memory) and comprise the
  1289.       *BIOS*, or basic input/output system of the computer.  Details of
  1290.       the BIOS routines can be found in Appendix A of IBM's Technical
  1291.       Reference Manual.  IBM charges around $40 for Technical
  1292.       Reference, but the information in Appendix A alone is easily
  1293.       worth the money.
  1294.  
  1295.       The routines do all kinds of useful things, such as run the disk
  1296.       drive for you, print characters on the screen, or read data from
  1297.       the keyboard.  In effect, the software interrupts add a whole
  1298.       series of very powerful operations to the 8088 instruction set.
  1299.  
  1300.  
  1301.  
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.                                                                      20
  1323.  
  1324.       A final point is that if you don't like the way that DOS or the
  1325.       BIOS does something, the vectored interrupt system makes it very
  1326.       easy to substitute your own program to handle that function.  You
  1327.       just load your program and reset the appropriate interrupt vector
  1328.       to point at your program rather than the resident routine.  This
  1329.       is how all those RAM disk and print spooler programs work.  The
  1330.       programs change the vector for disk drive or printer support to
  1331.       point to themselves, and carry out the operations in their own
  1332.       special way.
  1333.  
  1334.       To make things easy for you, one of the DOS interrupt routines
  1335.       has the function of resetting interrupt vectors to point at new
  1336.       code.  Still another DOS interrupt routine is used to graft new
  1337.       code onto DOS, so that it doesn't accidentally get wiped out by
  1338.       other programs.  The whole thing is really quite elegant and easy
  1339.       to use, and IBM is to be complimented for setting things up this
  1340.       way.
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.                                                                      21
  1389.  
  1390.       >>PSEUDO-OPERATIONS<<
  1391.  
  1392.       Up to this point, I've implied that each line of an assembly
  1393.       language program gets translated into a machine language
  1394.       instruction.  In fact, this is not the case.  Most assemblers
  1395.       recognize a series of *pseudo-operations* which are handled as
  1396.       embedded commands to the assembler itself, not as an instruction
  1397.       in the machine language program being built.  Almost invariably
  1398.       you'll see the phrase "pseudo-operation" abbreviated down to
  1399.       *pseudo-op*. Sometimes you'll see *assembler directive*, which
  1400.       means the same thing, but just doesn't seem to roll off the
  1401.       tongue as well as pseudo-op.
  1402.  
  1403.       One very common pseudo-op is the *equate*, usually given mnemonic
  1404.       *EQU*.  What this allows you to do is assign a name to a
  1405.       frequently used constant.  Thereafter, anywhere you use that
  1406.       name, the assembler automatically substitutes the equated
  1407.       constant.  This process makes your program easier to read, since
  1408.       in place of the somewhat meaningless looking pattern, you see a
  1409.       name which tells you what the pattern is for.  It also makes your
  1410.       program easier to modify, since if you decide to change the
  1411.       constant, you only need to do it once, rather than all over the
  1412.       program.
  1413.  
  1414.       The only other type of pseudo-op I'll talk about here are those
  1415.       for setting aside memory locations for data.  These pseudo-ops
  1416.       tend to be quite idiosyncratic with each assembler.  CHASM
  1417.       implements two such pseudo-ops: DB (declare byte) and DS (declare
  1418.       storage).  DB is used to set aside small data areas, which can be
  1419.       initialized to any pattern, one byte at a time.  DS sets up
  1420.       relatively large areas, but all the locations are filled with the
  1421.       same initial pattern.
  1422.  
  1423.       If you put a label on a pseudo-op which sets aside data areas,
  1424.       most assemblers allow you to use the label as an operand, in
  1425.       place of the actual address of the location.  The assembler
  1426.       automatically substitutes the address for the name during the
  1427.       translation process.
  1428.  
  1429.       Some assemblers have a great number of pseudo-ops.  CHASM
  1430.       implements a few more, which aren't discussed here.
  1431.  
  1432.  
  1433.  
  1434.  
  1435.  
  1436.  
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.                                                                      22
  1455.  
  1456.       >>TUTORIAL<<
  1457.  
  1458.       To conclude this primer, this section will walk through the
  1459.       process of writing, assembling, and running a very simple
  1460.       program.
  1461.  
  1462.       Our program will just print a message on the video screen, and
  1463.       then return to DOS.  Although very simple, this program will
  1464.       demonstrate a number of points, including a DOS function call,
  1465.       setting aside memory for storage, and good programming form.
  1466.  
  1467.       Appendix D of the DOS manual discusses the various DOS
  1468.       functions and interrupts available to the assembly language
  1469.       programmer.   To print a text string to the video screen, we'll
  1470.       use function 9.  You should read the documentation for this
  1471.       function in your DOS manual at this time.
  1472.  
  1473.  
  1474.       Did it make any sense?  To use this function, we have to load the
  1475.       DX register with the address of a string in memory, specify
  1476.       function 9 by loading a "9" into the AH register, then ask DOS to
  1477.       do the printing by executing interrupt 21H.  Basically, we just
  1478.       set things up, and DOS does all the real work.
  1479.  
  1480.       Here's the code to do this:
  1481.  
  1482.            MOV AH, 9                 ;specify DOS function 9
  1483.            MOV DX, OFFSET(MESSAGE)   ;get address of string
  1484.            INT 21H                   ;call DOS
  1485.  
  1486.       Note that none of the lines starts at the left margin  (column
  1487.       one). If they did, CHASM would think that the instruction
  1488.       mnemonic was meant to be a label, and would get very confused.
  1489.       Also note that each line has a comment explaining what's going
  1490.       on.
  1491.  
  1492.       The second line needs a little explaining.  CHASM's OFFSET
  1493.       function returns the address of whatever is included in the
  1494.       parentheses, in this case, MESSAGE.  The assumption made here is
  1495.       that later in the program we will set aside a memory location
  1496.       containing our string to be printed, and give it the name
  1497.       "MESSAGE".
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.                                                                      23
  1521.  
  1522.       Once we've printed our string, we want to return to DOS.  If we
  1523.       don't explicitly transfer control back to DOS, the 8088 will
  1524.       happily continue to execute whatever random patterns are in
  1525.       memory after our stuff.  Remember "crashing the system"?  One of
  1526.       DOS's reserved interrupts handles program termination, returning
  1527.       you to DOS.  The proper instruction is:
  1528.  
  1529.              INT 20H        ;return to DOS
  1530.  
  1531.       All that's left at this point is to set aside a chunk of memory
  1532.       containing a string to be printed.  We'll use CHASM's DB (Declare
  1533.       Bytes) pseudo-op to do this:
  1534.  
  1535.       MESSAGE DB  'Hello, World!$'     ;message to be printed
  1536.  
  1537.       The memory location is given the name "MESSAGE" because the line
  1538.       started with a "MESSAGE" as a label.  Now CHASM will know that
  1539.       the preceding OFFSET was talking about this memory location.  You
  1540.       don't need to worry about what the actual address of MESSAGE is,
  1541.       CHASM takes care of that.
  1542.  
  1543.       Fourteen bytes of memory get set aside here, containing the ASCII
  1544.       codes for the characters in "Hello, World!$".  Note that the
  1545.       string ends in the character "$".  DOS function 9 prints
  1546.       characters until it encounters a "$", at which point it stops.
  1547.       If you forget to put the "$" at the end of your string, you'll
  1548.       have the less-than-amusing experience of watching DOS attempt to
  1549.       print out the entire contents of memory.
  1550.  
  1551.  
  1552.  
  1553.  
  1554.  
  1555.  
  1556.  
  1557.  
  1558.  
  1559.  
  1560.  
  1561.  
  1562.  
  1563.  
  1564.  
  1565.  
  1566.  
  1567.  
  1568.  
  1569.  
  1570.  
  1571.  
  1572.  
  1573.  
  1574.  
  1575.  
  1576.  
  1577.  
  1578.  
  1579.  
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.                                                                      24
  1587.  
  1588.       Bringing everything together, and adding a few comments at the
  1589.       beginning, here's our complete program:
  1590.  
  1591.       ;=====================================;
  1592.       ; HELLO    Version 1.00               ;
  1593.       ;          1984 by David Whitman      ;
  1594.       ;                                     ;
  1595.       ; Sample source file for CHASM.       ;
  1596.       ; Prints a greeting on the console.   ;
  1597.       ;=====================================;
  1598.  
  1599.               MOV AH, 9                 ;specify DOS function 9
  1600.               MOV DX, OFFSET(MESSAGE)   ;get address of string
  1601.               INT 21H                   ;call DOS
  1602.  
  1603.               INT 20H                   ;return to DOS
  1604.  
  1605.       MESSAGE DB  'Hello, World!$'      ;message to be printed
  1606.  
  1607.       After writing all this, we need to create a text file which
  1608.       contains the lines of our program.  You do this with a text
  1609.       editor or word processor.  (Of course, in real life you write the
  1610.       program using the editor in the first place.)
  1611.  
  1612.       CHASM likes its source files in "standard DOS" format, what some
  1613.       word processors call "document" or "ASCII mode".  Most word
  1614.       processors, and all straight text editors work in this format
  1615.       automatically.  Wordstar and Easywriter (and probably a few other
  1616.       packages) have their own special formats, but their manual should
  1617.       tell you how to make standard DOS files.
  1618.  
  1619.       At this point, make a standard DOS file named HELLO.ASM which
  1620.       contains the above program lines.  If you're feeling lazy, or if
  1621.       you run into problems, the file EXAMPLE.ASM on your CHASM disk
  1622.       has these lines already entered for you.  Just copy EXAMPLE.ASM
  1623.       into a new file called HELLO.ASM and you're in business.
  1624.  
  1625.       It's now time to assemble the program.  To start out, you have to
  1626.       set up a CHASM disk.  Follow the directions in the User's Manual
  1627.       under "Setting up a CHASM Work Disk", or for now, just copy the
  1628.       file BASIC.COM from your DOS disk onto your CHASM distribution
  1629.       disk.  Copy HELLO.ASM onto this disk as well. 
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.                                                                      25
  1653.  
  1654.       Put the CHASM disk into your default drive, and start up CHASM by
  1655.       typing its name:
  1656.  
  1657.          A> CHASM
  1658.  
  1659.       CHASM will respond by printing a hello screen, and ask you to
  1660.       press a key when you're done reading it.  When you do so, CHASM
  1661.       will ask you some questions:
  1662.  
  1663.           Source code file name? [.asm]
  1664.  
  1665.       Type in the HELLO.ASM, or just HELLO, then hit return.  (If you
  1666.       don't enter the file extension, CHASM assumes that it's .ASM)
  1667.  
  1668.           Direct listing to Printer (P), Screen (S), or Disk (D)?
  1669.  
  1670.       CHASM wants to know where to send the listing produced during the
  1671.       assembly process.  If you have a printer, turn it on, and then
  1672.       press P.  If you don't have a printer, press S.
  1673.  
  1674.       The last question is:
  1675.  
  1676.           Name for object file? [hello.com]
  1677.  
  1678.       CHASM is asking for the name you'd like to give to the machine
  1679.       language program which is about to be produced.  Just press enter
  1680.       here.  CHASM will name the program HELLO.COM
  1681.  
  1682.       At this point CHASM will start accessing the disk drive, reading
  1683.       in your program a line at a time.  A status line will appear at
  1684.       the bottom of your screen, telling you how far along the
  1685.       translation has gotten.  For this program, the whole process
  1686.       takes about a minute.
  1687.  
  1688.       If the listing went to your printer, CHASM automatically returns
  1689.       you to DOS when it's finished.  If it went to the screen, CHASM
  1690.       waits for you to press a key to indicate that you're done
  1691.       reading.  Near the bottom of the listing will be the message:
  1692.  
  1693.       XXX Diagnostics Offered
  1694.       YYY Errors Detected
  1695.  
  1696.  
  1697.  
  1698.  
  1699.  
  1700.  
  1701.  
  1702.  
  1703.  
  1704.  
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.  
  1714.  
  1715.  
  1716.  
  1717.  
  1718.                                                                      26
  1719.  
  1720.       If both numbers are 0, everything went fine.  If not, look up on
  1721.       the listing for error messages, which will point out the
  1722.       offending lines.  At this point, don't worry too much about what
  1723.       the error messages say, just fix the line in your input file to
  1724.       look like the text developed above.  Once you manage to get an
  1725.       assembly with no errors, you're ready to go on.
  1726.  
  1727.       Your disk will now contain machine language program named
  1728.       HELLO.COM.  Confirm this by typing DIR to get a directory
  1729.       listing.  You should see the new program file listed.
  1730.  
  1731.       To run the machine language program, you just type its name,
  1732.       without the .COM extension.  (Note: even though you don't *enter*
  1733.       the "COM", the file has to have this extension for DOS to
  1734.       recognize it as a machine language program.)  Try it out now.
  1735.       From the DOS prompt, type: HELLO.  Your disk drive will whir for
  1736.       a second, then the message "Hello World!" will appear.
  1737.  
  1738.       For a further exercise, you might try printing a carriage return
  1739.       and then a line feed before the message, to space it down the
  1740.       screen a little.  Carriage return has ASCII code 13, and line
  1741.       feed is 10.  Read the CHASM User's Manual about the DB pseudo-op,
  1742.       and add these two characters at the beginning of STRING using
  1743.       their decimal representations.
  1744.  
  1745.       Try writing a new program called BEEP which writes the "bell"
  1746.       character to the screen.  You can use BEEP to signal the end of
  1747.       long batch files, or to annoy your co-workers.  Resist the urge
  1748.       to program a loop into BEEP.
  1749.  
  1750.       An even more advanced exercise would be to clear the screen
  1751.       before printing a message.  The easiest way to do this is to
  1752.       use a BIOS function, VIDEO_IO, documented on pages A-43 through
  1753.       A-44 of Technical Reference.  The comments in the BIOS listing
  1754.       tell you what values to load into which registers to get VIDEO_IO
  1755.       to monkey with the screen for you.  Load the registers and
  1756.       execute INT 10H, once to blank the screen, and again to
  1757.       move the cursor to the upper left hand corner.
  1758.  
  1759.       If you've read all of this primer and run the above program,
  1760.       maybe modifying it a little, you're no longer a rank beginner.
  1761.       At this point you should have enough of a start to be able to
  1762.       digest the CHASM User's Manual and The 8086 Book, then begin to
  1763.       write your own programs.  Good Luck!
  1764.  
  1765.  
  1766.  
  1767.  
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777.  
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.